package com.app.studyroom;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.studyroom.adapter.UserAdapter;
import com.app.studyroom.api.ApiClient;
import com.app.studyroom.api.ApiResponse;
import com.app.studyroom.api.ApiService;
import com.app.studyroom.api.model.ssrUser.PageUserListRequest;
import com.app.studyroom.api.model.ssrUser.PageUserListResponse;
import com.app.studyroom.api.model.ssrUser.UserDeleteRequest;
import com.app.studyroom.api.model.ssrUser.UserItem;
import com.app.studyroom.api.model.ssrUser.UserUpdateRequest;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserListActivity extends BaseActivity {

    private RecyclerView recyclerUserList;
    private UserAdapter userAdapter;
    private List<UserItem> userList = new ArrayList<>();
    private ApiService apiService;
    private String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);

        SharedPreferences sp = getSharedPreferences("Session", MODE_PRIVATE);
        token = sp.getString("token", "");

        recyclerUserList = findViewById(R.id.recyclerUserList);
        recyclerUserList.setLayoutManager(new LinearLayoutManager(this));
        userAdapter = new UserAdapter(this, userList, new UserAdapter.OnUserActionListener() {
            @Override
            public void onEdit(UserItem user) {
                updateUser(user);
            }

            @Override
            public void onDelete(long userId) {
                deleteUser(userId);
            }
        });
        recyclerUserList.setAdapter(userAdapter);

        apiService = ApiClient.getClient().create(ApiService.class);

        loadUserList();
    }

    private void loadUserList() {
        PageUserListRequest request = new PageUserListRequest(1, 50);

        apiService.pageUserList(token, request).enqueue(new Callback<PageUserListResponse>() {
            @Override
            public void onResponse(Call<PageUserListResponse> call, Response<PageUserListResponse> response) {
                if (response.isSuccessful() && response.body() != null && response.body().getData() != null) {
                    userList = response.body().getData().getData();
                    userAdapter.setUserList(userList);
                } else {
                    Toast.makeText(UserListActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<PageUserListResponse> call, Throwable t) {
                Toast.makeText(UserListActivity.this, "网络错误：" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void updateUser(UserItem user) {
        UserUpdateRequest updateRequest = new UserUpdateRequest();
        updateRequest.setId(user.getId());
        updateRequest.setUserName(user.getUserName());
        updateRequest.setUserSjh(user.getUserSjh());
        updateRequest.setUserBs(user.getUserBs());
        updateRequest.setQy(user.getQy());
        updateRequest.setUserNumber(user.getUserNumber());
        updateRequest.setUserZjh(user.getUserZjh());

        apiService.userUpdate(token, updateRequest).enqueue(new Callback<ApiResponse<Object>>() {
            @Override
            public void onResponse(Call<ApiResponse<Object>> call, Response<ApiResponse<Object>> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(UserListActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    loadUserList();
                } else {
                    Toast.makeText(UserListActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<Object>> call, Throwable t) {
                Toast.makeText(UserListActivity.this, "网络错误：" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void deleteUser(long userId) {
        UserDeleteRequest deleteRequest = new UserDeleteRequest(userId);

        apiService.userDelete(token, deleteRequest).enqueue(new Callback<ApiResponse<Object>>() {
            @Override
            public void onResponse(Call<ApiResponse<Object>> call, Response<ApiResponse<Object>> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(UserListActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                    loadUserList();
                } else {
                    Toast.makeText(UserListActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<Object>> call, Throwable t) {
                Toast.makeText(UserListActivity.this, "网络错误：" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
